#include "qpropertymanager.h"

#include "qabstractproperty.h"

using namespace SHAREDLIB;

QPropertyManager* QPropertyManager::m_manager=NULL;

QPropertyManager::QPropertyManager(QObject *parent) :
    QObject(parent)
{
    m_timer=new QTimer(this);
    connect(m_timer,SIGNAL(	timeout()),this,SLOT(checkProperty()));
    m_timer->start(30);
}

QPropertyManager* QPropertyManager::getPropertyManager()
{
    if(m_manager==NULL)
    {
        m_manager=new QPropertyManager;
    }

    return m_manager;
}

void QPropertyManager::releasePropertyManager()
{
    if(m_manager!=NULL)
    {
        delete m_manager;
        m_manager=NULL;
    }
}

void QPropertyManager::checkProperty()
{
    foreach(QAbstractProperty* pro,m_propertys)
    {
        if(pro->parent()==NULL)
        {
            if(pro->getShowValue()!=pro->getValue())
            {
                pro->setValue(pro->getValue());
            }
        }
    }
}

void QPropertyManager::addProperty(QAbstractProperty *property)
{
    if(property==NULL || m_propertys.contains(property))
    {
        return;
    }
    m_propertys.append(property);
    connect(property,SIGNAL(destroyed()),this,SLOT(propertyDestory()));
}

void QPropertyManager::propertyDestory()
{
    m_propertys.removeAll((QAbstractProperty*)sender());
    if(m_propertys.size()==0)
    {
        releasePropertyManager();
    }
}
